<?php
/**
 * Base.php
 * Author: luoji (867404364@qq.com)
 * Date: 2018-02-01
 * Time: 17:21
 */

namespace App\Http\Controllers;


use Illuminate\Http\Request;

class Base extends Controller
{
    const RES_STATUS_SUCCESS = 1;
    const RES_STATUS_FAIL = 0;
    const RES_STATUS_NO_LOGIN = -1;

    const TOKEN_KEY = 'HTTP_X_TOKEN';
    const UID_KEY = 'HTTP_X_UID';
    /**
     * @var Request Illuminate\Http\Request;
     */
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 返回JSON数据正确信息(框架会自动转)
     * @param string $msg
     * @param string $data
     * @return array
     */
    protected function responseSuccess($data = [], string $msg = 'success')
    {
        $responseData = ['status' => (int) self::RES_STATUS_SUCCESS, 'msg' => $msg, 'rdata' => $data];
        return response()->json($responseData);
    }

    /**
     * 返回错误信息
     * @param string $data
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function responseFail(string $msg = "fail", $data = [])
    {
        $responseData = ['status' => (int) self::RES_STATUS_FAIL, 'msg' => $msg, 'rdata' => $data];
        return response()->json($responseData);
    }

    /**
     * 获取头部中的TOKEN值
     * @return mixed
     */
    public static function getToken()
    {
        return request()->server(self::TOKEN_KEY) ?? '';
    }

    public static function getUID()
    {
        return request()->server(self::UID_KEY) ?? 0;
    }

}